<?php

/**
 * uniBoard API Settings
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-08 03:38:35 +0000 (Wed, 08 Sep 2010) $
 * $Revision: 60 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class api_settings extends class_api{
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore			= NULL;
	
	/**
	 * Settings group id
	 *
	 * @var integer
	 */
	
	public $group_id 			= 0;
	
	/**
	 * Old group key
	 *
	 * @var string
	 */
	
	public $group_key_old 		= '';
	
	/**
	 * Settings group key
	 *
	 * @var string
	 */
	
	public $group_key 			= '';
	
	/**
	 * Settings group name
	 *
	 * @var string
	 */
	
	public $group_name 		= '';
	
	/**
	 * Settings group description
	 *
	 * @var string
	 */
	
	public $group_description	= '';
	
	/**
	 * Settings group items
	 *
	 * @var integer
	 */
	
	public $group_items 		= 0;
	
	// ------------------------------------------------------------------------

	/**
	 * Initialise Api
	 *
	 * @param object $appCore
	 * @param integer $target_id
	 * @param string $target_key
	 */
	
	function __construct( $appCore, $target_id = 0, $target_key = ''){
					
		// Store appCore pointer
		$this -> appCore = $appCore;
	
		// Store group id and key
		$this -> group_id 	= (integer) $target_id;
		$this -> group_key 	= $target_key;
		
		// Autoload If Group ID is bigger than zero or key is not empty
		if ( $target_id > 0 || isset( $target_key[0]))
		{
			// Make select
			$appCore -> db -> query( 'SELECT *
			FROM ' . DB_PREFIX . 'settings_groups
			WHERE ' . ( $this -> group_id > 0 ? 'settings_group_id = \'' . $this -> group_id . '\'' : 'settings_group_key = \'' . $appCore -> db -> strEscape( $this -> group_key). '\'
			LIMIT 1'));
			
			// Settings found
			if ( $group_result = $appCore -> db -> fetch_assoc())
			{
				$this -> data_loaded			= true;
				
				$this -> group_id 				= $group_result['settings_group_id'];
				$this -> group_key 				= $group_result['settings_group_key'];
				$this -> group_key_old			= $group_result['settings_group_key'];
				$this -> group_name 			= $group_result['settings_group_name'];
				$this -> group_description		= $group_result['settings_group_description'];
				$this -> group_items	 		= $group_result['settings_group_items'];
			}
			else
			{
				// Set error
				$this -> setError(  $appCore -> lang['acp_settings_group_not_found']);
			}
			
			// Free result
			$appCore -> db -> freeResult();
		}
		
	}
		
	// ------------------------------------------------------------------------

	/**
	 * Save API data to DB
	 *
	 */
	
	function saveData(){
		
		// Do mysql query only when we have changes, and no errors
		if ( !empty( $this -> changes) && !$this -> error)
		{	
			// Are we changing group key?
			if ( isset( $this -> changes['settings_group_key']))
			{
				// Update key in settings
				$this -> appCore -> db -> update( array( 'setting_group' => $this -> appCore -> db -> strEscape( $this -> group_key)), 'settings', 'setting_group = \'' . $this -> appCore -> db -> strEscape( $this -> group_key_old) . '\'');
			}
			
			// Insert?
			if ( $this -> group_id == 0)
			{
				// Insert new record
				$this -> appCore -> db -> insert( $this -> changes, 'settings_groups');
			
				// Store new group id
				$this -> group_id = $this -> appCore -> db -> insertId();
			}
			else
			{
				// Update record
				$this -> appCore -> db -> update( $this -> changes, 'settings_groups', 'settings_group_id = \'' . $this -> group_id . '\'');
			}
			
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Delete API data from DB
	 *
	 */
	
	function deleteData(){
		
		// Delete settings group, but leave settings
		$this -> appCore -> db -> delete( 'settings_groups', 'settings_group_id = \'' . $this -> group_id . '\'');
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Set group_key
	 *
	 * @param string $new_value
	 */
	
	function setGroupKey( $new_value){
		
		// Fix new value
		$new_value = strtolower( $this -> appCore -> secureFilename( $new_value));
		
		// Empty?
		if ( isset(  $new_value[0]))
		{
			// Store in object
			$this -> group_key = $new_value;
		
			// Keyword too long
			if ( mb_strlen( $new_value) > 255)
			{
				// Group key is empty
				$this -> setError( $this -> appCore -> lang['acp_settings_group_key_too_long']);
			}
			else
			{
				// Add to made changes
				$this -> changes['settings_group_key'] = $this -> appCore -> db -> strEscape( $this -> group_key);
			}
		}
		else
		{
			// Group key is empty
			$this -> setError( $this -> appCore -> lang['acp_settings_group_key_empty']);
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Set group_name
	 *
	 * @param string $group_name
	 */
	
	function setGroupName( $group_name){
		
		if ( isset( $group_name[0]))
		{
			// Store in object
			$this -> group_name = $group_name;
						
			// Keyword too long
			if ( mb_strlen( $new_value) > 255)
			{
				// Group key is empty
				$this -> setError( $this -> appCore -> lang['acp_settings_group_name_too_long']);
			}
			else
			{
				// Add to made changes
				$this -> changes['settings_group_name'] = $this -> appCore -> db -> strEscape( $this -> group_name);
			}		
		}else
		{
			// Set error
			$this -> setError(  $this -> appCore -> lang['acp_settings_group_name_empty']);
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Set group_description
	 *
	 * @param string $new_value
	 */
	
	function setGroupDescription( $new_value){
	
		// Store in object
		$this -> group_description = $new_value;
		
		// Add to made changes
		$this -> changes['settings_group_description'] = $this -> appCore -> db -> strEscape( $new_value);
				
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Set group_settings
	 *
	 * @param integer $new_value
	 */
	
	function setGroupSettings( $new_value){
	
		// Store in object
		$this -> group_settings = (int) $new_value;
		
		// Add to made changes
		$this -> changes['settings_group_settings'] = $this -> group_settings;
				
	}
	
}